package com.kong.controller;

import com.kong.constant.MessageConstant;
import com.kong.entity.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yang2013
 * @data 2020/10/18 17:42
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/getUserName")
    public Result getUserName(){
        try {
            //登陆成功后，对象信息存储再security中
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            return new Result(true,MessageConstant.GET_USERNAME_SUCCESS, username);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }
}
